import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AppConfig, DatabaseConfig, RedisConfig, JwtConfig, LoggingConfig, FeaturesConfig } from '@/common/config.interface';

@Injectable()
export class AppConfigService {
    constructor(private readonly configService: ConfigService) {}

    /**
     * 获取应用配置
     */
    getAppConfig(): AppConfig {
        return {
            nodeEnv: this.getNodeEnv(),
            port: this.getPort(),
            database: this.getDatabaseConfig(),
            redis: this.getRedisConfig(),
            jwt: this.getJwtConfig(),
            logging: this.getLoggingConfig(),
            features: this.getFeaturesConfig(),
        };
    }

    /**
     * 获取节点环境
     */
    getNodeEnv(): string {
        return this.configService.get<string>('NODE_ENV', 'development');
    }

    /**
     * 获取端口
     */
    getPort(): number {
        return this.configService.get<number>('APP_PORT', 3000);
    }

    /**
     * 获取数据库配置
     */
    getDatabaseConfig(): DatabaseConfig {
        return {
            host: this.configService.get<string>('DB_HOST', 'localhost'),
            port: this.configService.get<number>('DB_PORT', 3306),
            username: this.configService.get<string>('DB_USERNAME', 'root'),
            password: this.configService.get<string>('DB_PASSWORD', 'password'),
            database: this.configService.get<string>('DB_DATABASE', 'nest_demo'),
            synchronize: this.configService.get<boolean>('DB_SYNCHRONIZE', false),
            logging: this.configService.get<boolean>('DB_LOGGING', false),
            dropSchema: this.configService.get<boolean>('DB_DROP_SCHEMA', false),
        };
    }

    /**
     * 获取 Redis 配置
     */
    getRedisConfig(): RedisConfig {
        return {
            host: this.configService.get<string>('REDIS_HOST', 'localhost'),
            port: this.configService.get<number>('REDIS_PORT', 6379),
            password: this.configService.get<string>('REDIS_PASSWORD', ''),
            db: this.configService.get<number>('REDIS_DB', 0),
            maxRetries: this.configService.get<number>('REDIS_MAX_RETRIES', 3),
        };
    }

    /**
     * 获取 JWT 配置
     */
    getJwtConfig(): JwtConfig {
        return {
            secret: this.configService.get<string>('JWT_SECRET', 'EHvvrvZ639twaCePmjVUBwHCdhJY5mr8'),
            expiresIn: this.configService.get<string>('JWT_EXPIRES_IN', '24h'),
        };
    }

    /**
     * 获取日志配置
     */
    getLoggingConfig(): LoggingConfig {
        return {
            level: this.configService.get<string>('LOG_LEVEL', 'info'),
            dbLogLevel: this.configService.get<string>('DB_LOG_LEVEL', 'warn'),
            cacheLogLevel: this.configService.get<string>('CACHE_LOG_LEVEL', 'info'),
            format: this.configService.get<string>('LOG_FORMAT', 'json'),
        };
    }

    /**
     * 获取功能开关配置
     */
    getFeaturesConfig(): FeaturesConfig {
        return {
            swagger: this.configService.get<boolean>('ENABLE_SWAGGER', false),
            debug: this.configService.get<boolean>('ENABLE_DEBUG', false),
        };
    }

    /**
     * 检查是否为开发环境
     */
    isDevelopment(): boolean {
        return this.getNodeEnv() === 'development';
    }

    /**
     * 检查是否为生产环境
     */
    isProduction(): boolean {
        return this.getNodeEnv() === 'production';
    }

    /**
     * 检查是否为测试环境
     */
    isTest(): boolean {
        return this.getNodeEnv() === 'test';
    }

    /**
     * 检查是否为预发布环境
     */
    isStaging(): boolean {
        return this.getNodeEnv() === 'staging';
    }

    /**
     * 获取环境信息摘要
     */
    getEnvironmentSummary(): string {
        const config = this.getAppConfig();
        return `
🌍 环境配置摘要:
   环境: ${config.nodeEnv}
   端口: ${config.port}
   数据库: ${config.database.host}:${config.database.port}/${config.database.database}
   Redis: ${config.redis.host}:${config.redis.port}
   日志级别: ${config.logging.level}
   Swagger: ${config.features.swagger ? '启用' : '禁用'}
   Debug: ${config.features.debug ? '启用' : '禁用'}
    `.trim();
    }
}
